package com.gh.ssyx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gh.ssyx.common.result.Result;
import com.gh.ssyx.model.acl.Admin;
import com.gh.ssyx.service.AdminService;
import com.gh.ssyx.service.RoleService;
import com.gh.ssyx.util.MD5;
import com.gh.ssyx.vo.acl.AdminQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Eric Fang
 * @date 2023/6/18 16:36
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/admin/acl/user")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;

    //1 用户列表
    @ApiOperation("用户列表")
    @GetMapping("/{pageNum}/{pageSize}")
    public Result getPageList(AdminQueryVo adminQueryVo,
                              @PathVariable Long pageNum,
                              @PathVariable Long pageSize){
        Page<Admin> pageParam = new Page<>(pageNum,pageSize);
        IPage<Admin> iPage = adminService.getPageList(pageParam,adminQueryVo);
        return Result.ok(iPage);
    }

    //2 id 查询用户
    @ApiOperation("通过id查询用户")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id){
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }

    //3 添加用户
    @ApiOperation("添加用户")
    @PostMapping("/save")
    public Result saveAdmin(@RequestBody Admin admin){
        String password = admin.getPassword();
        String encrypt = MD5.encrypt(password);
        admin.setPassword(encrypt);
        boolean save = adminService.save(admin);
        return save ? Result.ok(null) : Result.fail(null);
    }

    //4 修改用户
    @ApiOperation("修改用户")
    @PutMapping("/update")
    public Result updateAdmin(@RequestBody Admin admin){
        if(!StringUtils.isEmpty(admin.getPassword())){
            admin.setPassword(MD5.encrypt(admin.getPassword()));
        }
        boolean b = adminService.updateById(admin);
        return b ? Result.ok(null) : Result.fail(null);
    }

    //5 根据id删除
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除某个用户")
    public Result removeById(@PathVariable Long id){
        boolean b = adminService.removeById(id);
        return b ? Result.ok(null) : Result.fail(null);
    }

    @DeleteMapping("/batchRemove")
    @ApiOperation("批量删除多个用户")
    public Result batchRemove(@RequestBody List<Long> ids){
        boolean b = adminService.removeByIds(ids);
        return b ? Result.ok(null) : Result.fail(null);
    }

    //6 获取某个用户的所有角色
    @ApiOperation("获取某个用户的所有角色")
    @GetMapping("/toAssign/{adminId}")
    public Result getRoles(@PathVariable Long adminId){
        Map<String,Object> map = roleService.getRoles(adminId);
        return Result.ok(map);
    }

    //7 给某个用户分配角色
    @ApiOperation("给某个用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long adminId,
                           @RequestParam("roleId") Long[] roleIds){
        roleService.saveAdminRole(adminId,roleIds);
        return Result.ok(null);
    }

}
